/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.menosehmais.framework.configuration;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.ComponentFactory;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Quintino
 */
@Component
@ApplicationScoped
public class CustomEntityManagerFactoryCreator implements ComponentFactory<EntityManagerFactory> {

    private EntityManagerFactory factory;

    @PostConstruct
    public void create() {
        factory = Persistence.createEntityManagerFactory(PersistenceUnit.DEFAULT);
    }

    @Override
    public EntityManagerFactory getInstance() {
        return factory;
    }

    @PreDestroy
    public void destroy() {
        factory.close();
    }
}
